package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;

import com.cloudsigns.factorymanager2.engineering.IView.IPatrolOrderAssignReceiveDetailView;
import com.cloudsigns.factorymanager2.engineering.IView.IPatrolOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.adapter.DispatchPatrolOrderAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.Dispatchbean;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.UserInfoBean;
import com.cloudsigns.factorymanager2.engineering.presenter.PatrolOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by tony on 2018/5/18.
 */

public class DispatchPatrolOrderActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener, IPatrolOrderDetailView, IPatrolOrderAssignReceiveDetailView {
    private View rl_back;
    private TextView tv_title;
    private TextView tv_project_name, tv_engineering_name;
    private View rl_engineering_name;
    private View rl_add_product;
    private ListView lv;
    private ImageView img_edit_dispatch_information;
    private DispatchPatrolOrderAdapter adapter;
    private ArrayList<MediaBean> datas = new ArrayList<>();
    private HashMap<String, ArrayList<MediaBean>> dataMap = new HashMap<>(); //id
    //    List<HashMap> maplist = new ArrayList<>();
    private OrderParentBean orderParentBean;
    private final String TAG = DispatchPatrolOrderActivity.class.getSimpleName();
    private String from;
    private PatrolOrderDetailPresenter patrolOrderDetailPresenter;
    //    private PatrolOrderAssignReceiveDetailPresenter patrolOrderAssignReceiveDetailPresenter;
    private int selectedItemPosition = -1;
    private PatrolOrderBean patrolOrderBean;
//    private UserInfoBean userInfoBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_dispatch_patrol_order);

        patrolOrderDetailPresenter = new PatrolOrderDetailPresenter(this);
//        patrolOrderAssignReceiveDetailPresenter = new PatrolOrderAssignReceiveDetailPresenter(this);
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            from = mIntent.getStringExtra(ConstantUtil.KEY_1);
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

            if (null != orderParentBean) {
                tv_project_name.setText(orderParentBean.getProjectName());
            }
        }

        patrolOrderDetailPresenter.getPatrolOrderDetail();
//        patrolOrderAssignReceiveDetailPresenter.getPatrolOrderAssignReceiveDetail();
//        initData();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        rl_engineering_name = findViewById(R.id.rl_engineering_name);
        rl_add_product = findViewById(R.id.rl_add_product);
        lv = findViewById(R.id.lv);
        img_edit_dispatch_information=findViewById(R.id.img_edit_dispatch_information);

        tv_title.setText("巡查");
        rl_back.setOnClickListener(this);
        rl_engineering_name.setOnClickListener(this);
        img_edit_dispatch_information.setOnClickListener(this);

        adapter = new DispatchPatrolOrderAdapter(this, TAG);
        adapter.setData(datas);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);

    }


    private void initData(PatrolOrderBean tempOrderBean) {
        if (null != tempOrderBean) {

            tv_project_name.setText(tempOrderBean.getRegionName());

            if (null == patrolOrderBean) {
                patrolOrderBean = tempOrderBean;
                //tv_project_name.setText(patrolOrderBean.getRegionName());
                updateUi();
            } else {
                /*保留之前列表中未分派的数据*/
                List<MediaBean> tempMediaBeanList = tempOrderBean.getMediaList();
                List<MediaBean> oldMediaBeanList = patrolOrderBean.getMediaList();
                for (int i = 0; i < tempMediaBeanList.size(); i++) {

                    MediaBean tempMediaBean = tempMediaBeanList.get(i);
                    CommonEnum.AssignStatus assignStatus = tempMediaBean.getAssignStatus();
                    String assignReceiveOrderId = tempMediaBean.getAssignReceiveOrderId();

                    if (null != assignStatus) {
                        if (assignStatus == CommonEnum.AssignStatus.CREATED || assignStatus == CommonEnum.AssignStatus.EDITED) {
                            for (MediaBean mediaBean : oldMediaBeanList) {
                                if (mediaBean.getId().equals(tempMediaBean.getId())) {
                                    tempMediaBeanList.set(i, mediaBean);
                                }
                            }
                        }
                    }
                }

                patrolOrderBean = tempOrderBean;

                updateUi();

            }
        }


    }

    private void updateUi() {
        if (null != patrolOrderBean) {
            List<MediaBean> mediaBeanList = patrolOrderBean.getMediaList();
            if (null != mediaBeanList) {
                datas.clear();
                dataMap.clear();


                for (int i = 0; i < mediaBeanList.size(); i++) {
                    MediaBean mediaBean = mediaBeanList.get(i);


                    CommonEnum.AssignStatus assignStatus = mediaBean.getAssignStatus();
                    String assignReceiveOrderId = mediaBean.getAssignReceiveOrderId();

                    if (null != assignStatus) {
                        if (assignStatus == CommonEnum.AssignStatus.CREATED || assignStatus == CommonEnum.AssignStatus.EDITED) {
                            if (mediaBean.isChecked() && mediaBean.isSend()) {
                                if (dataMap.containsKey(assignReceiveOrderId)) {
                                    dataMap.get(assignReceiveOrderId).add(mediaBean);
                                } else {
                                    ArrayList<MediaBean> mediaBeanList1 = new ArrayList<>();
                                    mediaBeanList1.add(mediaBean);
                                    dataMap.put(assignReceiveOrderId, mediaBeanList1);
                                    datas.add(mediaBean);
                                }
                            }
                        } else if (assignStatus == CommonEnum.AssignStatus.SEND) {
                            if (dataMap.containsKey(assignReceiveOrderId)) {
                                dataMap.get(assignReceiveOrderId).add(mediaBean);
                            } else {
                                ArrayList<MediaBean> mediaBeanList1 = new ArrayList<>();
                                mediaBeanList1.add(mediaBean);
                                dataMap.put(assignReceiveOrderId, mediaBeanList1);
                                datas.add(mediaBean);
                            }

                        } else if (assignStatus == CommonEnum.AssignStatus.RECEIVED) {
                            if (dataMap.containsKey(assignReceiveOrderId)) {
                                dataMap.get(assignReceiveOrderId).add(mediaBean);
                            } else {
                                ArrayList<MediaBean> mediaBeanList1 = new ArrayList<>();
                                mediaBeanList1.add(mediaBean);
                                dataMap.put(assignReceiveOrderId, mediaBeanList1);

                                datas.add(mediaBean);
                            }

                        } else if (assignStatus == CommonEnum.AssignStatus.RETURNED) {
                            if (dataMap.containsKey(assignReceiveOrderId)) {
                                dataMap.get(assignReceiveOrderId).add(mediaBean);
                            } else {
                                ArrayList<MediaBean> mediaBeanList1 = new ArrayList<>();
                                mediaBeanList1.add(mediaBean);
                                dataMap.put(assignReceiveOrderId, mediaBeanList1);
                                datas.add(mediaBean);
                            }

                        } else if (assignStatus == CommonEnum.AssignStatus.COMPLETED) {
                            if (dataMap.containsKey(assignReceiveOrderId)) {
                                dataMap.get(assignReceiveOrderId).add(mediaBean);
                            } else {
                                ArrayList<MediaBean> mediaBeanList1 = new ArrayList<>();
                                mediaBeanList1.add(mediaBean);
                                dataMap.put(assignReceiveOrderId, mediaBeanList1);
                                datas.add(mediaBean);
                            }
                        }
                    }
                }
            }
        }
    }

    private void initData() {
        if (null != patrolOrderBean) {
            tv_project_name.setText(patrolOrderBean.getRegionName());

            List<MediaBean> mediaBeanList = patrolOrderBean.getMediaList();
            if (null != mediaBeanList) {
                datas.clear();

                for (MediaBean mediaBean : mediaBeanList) {
                    CommonEnum.AssignStatus assignStatus = mediaBean.getAssignStatus();
                    if (null != assignStatus) {
                        if (assignStatus == CommonEnum.AssignStatus.CREATED) {

                        } else if (assignStatus == CommonEnum.AssignStatus.EDITED) {

                        } else if (assignStatus == CommonEnum.AssignStatus.SEND) {
                            datas.add(mediaBean);
                        } else if (assignStatus == CommonEnum.AssignStatus.RECEIVED) {
                            datas.add(mediaBean);
                        } else if (assignStatus == CommonEnum.AssignStatus.RETURNED) {
                            datas.add(mediaBean);
                        } else if (assignStatus == CommonEnum.AssignStatus.COMPLETED) {
                            datas.add(mediaBean);

                        }
                    }
                }
            }

            adapter.setData(datas);
            adapter.notifyDataSetChanged();

        }
    }

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.rl_engineering_name:
                mIntent = new Intent(DispatchPatrolOrderActivity.this, PatrolOrderDetailActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                mIntent.putExtra(ConstantUtil.KEY_2, patrolOrderBean);
                startActivity(mIntent);
                break;
            case R.id.img_edit_dispatch_information:
                mIntent = new Intent(DispatchPatrolOrderActivity.this, EditDispatchInfomationForPatrolActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, CommonEnum.ROLECATEGORY.TEAM);
                mIntent.putExtra(ConstantUtil.KEY_2, patrolOrderBean);
//                mIntent.putExtra("selecteddata", datas);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;


        }
    }

    @Override
    protected void onActivityResult(int requestCode,
                                    int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {

                    patrolOrderBean = (PatrolOrderBean) data.getSerializableExtra(ConstantUtil.KEY_1);
//                    userInfoBean = (UserInfoBean) data.getSerializableExtra(ConstantUtil.KEY_2);
                    updateUi();

//                    datas.clear();
//
//                    ArrayList<MediaBean> mediaBeanList = (ArrayList<MediaBean>) data.getSerializableExtra("selecteddata");
////                    MediaBean dispatchbean = (Dispatchbean) data.getSerializableExtra("selecteddata");
////                    HashMap<String, List<MediaBean>> selectedBeanMap = (HashMap<String, List<MediaBean>>) data.getSerializableExtra(ConstantUtil.KEY_2);
//                    datas.addAll(mediaBeanList);
//                    adapter.setData(datas);
//                    adapter.notifyDataSetChanged();
////                    maplist.add(selectedBeanMap);


                }
                break;
            case ConstantUtil.START_ACTIVITY_KEY_2:
                if (null != data) {
                    boolean isdelete = data.getBooleanExtra(ConstantUtil.KEY_2, false);
                    if (isdelete) { //删除成功 由于服务器接口未提供 暂时只能删除未分派的点位
                        MediaBean mediaBean = datas.get(selectedItemPosition);
                        ArrayList<MediaBean> mediaBeanArrayList = dataMap.get(mediaBean.getAssignReceiveOrderId());
                        List<MediaBean> mediaBeanList = patrolOrderBean.getMediaList();
                        for (MediaBean mediaBean1 : mediaBeanArrayList) {
                            for (int i = 0; i < mediaBeanList.size(); i++) {
                                MediaBean mediaBean2 = mediaBeanList.get(i);
                                if (mediaBean1.getId().equals(mediaBean2.getId())) {
                                    mediaBean2.setChecked(false);
                                    mediaBean2.setSend(false);
                                }
                            }
                        }

                        updateUi();

                    } else { //派发成功
                        patrolOrderDetailPresenter.getPatrolOrderDetail();
//                        setResult(ConstantUtil.START_ACTIVITY_KEY_1, new Intent());
//                        finish();
                    }

                }

//                if (data != null) {
//                    boolean bool = data.getBooleanExtra(ConstantUtil.KEY_1, false);
//                    if (bool) {
//                        if (selectedItemPosition >= 0) {
//                            datas.remove(selectedItemPosition);
//                            adapter.setData(datas);
//                            adapter.notifyDataSetChanged();
//                            maplist.remove(selectedItemPosition);
//                        }
//                    }
//
//                }
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view,
                            int position, long l) {

        selectedItemPosition = position;
        MediaBean mediaBean = datas.get(position);
        for (String key : dataMap.keySet()) {
            if (key.equals(mediaBean.getAssignReceiveOrderId())) {
                Intent mIntent = new Intent(DispatchPatrolOrderActivity.this, DispatchPatrolOrderDetailActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, dataMap.get(key));
                mIntent.putExtra(ConstantUtil.KEY_2, patrolOrderBean);
//        mIntent.putExtra(ConstantUtil.KEY_2, maplist.get(position));
                mIntent.putExtra("orderId", orderParentBean.getId());

                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_2);
            }
        }

    }

    @Override
    public String getItemId() {
        return null;
    }

    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return null;
    }

    @Override
    public void getPatrolOrderAssignReceiveDetailSuccessed(PatrolOrderBean
                                                                   patrolOrderBean) {
//        this.patrolOrderBean = patrolOrderBean;
        initData(patrolOrderBean);
    }

    @Override
    public void getPatrolOrderAssignReceiveDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getPatrolOrderDetailSuccessed(PatrolOrderBean patrolOrderBean) {

        this.patrolOrderBean = patrolOrderBean;
        initData();

    }

    @Override
    public void gPatrolOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
